﻿using System;
using System.Collections.Generic;
using System.Text;
using OilDB.DataSetOilManagementTableAdapters;
using System.Data.SqlClient;
using System.Data;

namespace OilDB
{
    public class DataServices
    {
        private DataSetOilManagement dsOilManagement;

        private CompaniesTableAdapter adtCompanies;
        private CustomersTableAdapter adtCustomers;
        private ExpensesTableAdapter adtExpenses;
        private ImportsTableAdapter adtImports;
        private ProductsTableAdapter adtProducts;
        private PaymentsTableAdapter adtPayments;
        private SellsTableAdapter adtSells;
        private StoresTableAdapter adtStores;
        private DebtsTableAdapter adtDebts;
        private DispensersTableAdapter adtDis;
        private ReceiptsTableAdapter adtRec;



        private TableAdapterManager managerOfAdts;

        private ImportStatisticsTableAdapter adtImportStatistics;
        private ImportStatisticsEachDayTableAdapter adtImportStatisticsEachDay;
        private ImportStatisticsAllTableAdapter adtImportStatisticsAll;
        private ExportStatisticsTableAdapter adtExportStatistics;
        private ExportStatisticsEachDayTableAdapter adtExportStatisticsEachDay;
        private ExportStatisticsAllTableAdapter adtExportStatisticsAll;
        private ReceiptStatisticsEachDayTableAdapter adtReceiptStatisticsEachDay;
        private SellStatisticsTableAdapter adtSellStatistics;
        private SellStatistics_AllTableAdapter adtSellStatistics_All;
        private DebtStatisticsTableAdapter adtDebtStatistics;
        private DebtStatistics_AllTableAdapter adtDebtStatisticsAll;
        private DebtStatistics_CompanyTableAdapter adtDebtStatisticsCompany;
        private ExpenseStatisticsTableAdapter adtExpensesStatistics;
        private StoreStatisticsTableAdapter adtStoreStatistics;
        private PaymentStatisticsTableAdapter adtPaymentStatistic;

        private NumOfDispensersTableAdapter adtNumOfDis;


        //used
        private IndexForDispensersTableAdapter adtIndexers;

        private GetUserListTableAdapter adtGetUserList;
        private string connectionStr;
        private Guid currentStore;
        private string currentStoreName;

        public TableAdapterManager adapters { get { return managerOfAdts; } }
        public ImportStatisticsTableAdapter ImportStatisticsAdapter { get { return adtImportStatistics; } }
        public ImportStatisticsEachDayTableAdapter ImportStatisticsEachDayAdapter { get { return adtImportStatisticsEachDay; } }
        public ImportStatisticsAllTableAdapter ImportStatisticsAllAdapter { get { return adtImportStatisticsAll; } }
        public ExportStatisticsTableAdapter ExportStatisticsAdapter { get { return adtExportStatistics; } }
        public ExportStatisticsEachDayTableAdapter ExportStatisticsEachDayAdapter { get { return adtExportStatisticsEachDay; } }
        public ExportStatisticsAllTableAdapter ExportStatisticsAll { get { return adtExportStatisticsAll; } }
        public ReceiptStatisticsEachDayTableAdapter ReceiptStatisticsEachDayAdapter { get { return adtReceiptStatisticsEachDay; } }
        public SellStatisticsTableAdapter SellStatisticsAdapter { get { return adtSellStatistics; } }
        public SellStatistics_AllTableAdapter SellStatisticsAllAdapter { get { return adtSellStatistics_All; } }
        public DebtStatisticsTableAdapter DebtStatisticsAdapter { get { return adtDebtStatistics; } }
        public DebtStatistics_AllTableAdapter DebtStatisticsAllAdapter { get { return adtDebtStatisticsAll; } }
        public DebtStatistics_CompanyTableAdapter DebtStatisticsCompanyAdapter { get { return adtDebtStatisticsCompany; } }
        public ExpenseStatisticsTableAdapter ExpensesStatisticsAdapter { get { return adtExpensesStatistics; } }
        public StoreStatisticsTableAdapter StoreStatisticsAdapter { get { return adtStoreStatistics; } }
        public PaymentStatisticsTableAdapter PaymentStatisticAdapter { get { return adtPaymentStatistic; } }        
        public GetUserListTableAdapter GetUserListAdapter { get { return adtGetUserList; } }
        public NumOfDispensersTableAdapter NumOfDispensersAdapter
        {
            get
            {
                return adtNumOfDis;
            }
        }
        public IndexForDispensersTableAdapter IndexForDispensersAdapter { get { return adtIndexers; } }
        public ReceiptsTableAdapter ReceiptsAdapter { get { return adtRec; } }

        public DataServices(String connectionString, Guid Store, bool autoLoadData = true)
        {
            this.connectionStr = connectionString;
            dsOilManagement = new DataSetOilManagement();

            adtCompanies = new CompaniesTableAdapter { Connection = new SqlConnection(connectionString), ClearBeforeFill = true };
            adtCustomers = new CustomersTableAdapter { ClearBeforeFill = true, Connection = new SqlConnection(connectionString) };
            adtStores = new StoresTableAdapter { Connection = new SqlConnection(connectionString), ClearBeforeFill = true };
            adtExpenses = new ExpensesTableAdapter { Connection = new SqlConnection(connectionString), ClearBeforeFill = true };
            adtProducts = new ProductsTableAdapter { ClearBeforeFill = true, Connection = new SqlConnection(connectionString) };
            adtImports = new ImportsTableAdapter { ClearBeforeFill = true, Connection = new SqlConnection(connectionString) };
            adtPayments = new PaymentsTableAdapter { ClearBeforeFill = true, Connection = new SqlConnection(connectionString) };
            adtSells = new SellsTableAdapter { ClearBeforeFill = true, Connection = new SqlConnection(connectionString) };
            adtDebts = new DebtsTableAdapter { ClearBeforeFill = true, Connection = new SqlConnection(connectionString) };
            adtDis = new DispensersTableAdapter { ClearBeforeFill = true, Connection = new SqlConnection(connectionString) };
            adtRec = new ReceiptsTableAdapter { ClearBeforeFill = true, Connection = new SqlConnection(connectionString) };

            managerOfAdts = new TableAdapterManager { Connection = new SqlConnection(connectionString), 
                                                        CompaniesTableAdapter = adtCompanies, 
                                                        CustomersTableAdapter = adtCustomers, 
                                                        ExpensesTableAdapter = adtExpenses, 
                                                        ImportsTableAdapter = adtImports, 
                                                        ProductsTableAdapter = adtProducts, 
                                                        PaymentsTableAdapter = adtPayments, 
                                                        SellsTableAdapter = adtSells, 
                                                        StoresTableAdapter = adtStores, 
                                                        DebtsTableAdapter = adtDebts,
                                                        DispensersTableAdapter = adtDis,
                                                        ReceiptsTableAdapter = adtRec
                                                    };


            //statistic
            adtImportStatistics = new ImportStatisticsTableAdapter { ClearBeforeFill = true, Connection = new SqlConnection(connectionString) };
            adtImportStatisticsEachDay = new ImportStatisticsEachDayTableAdapter { ClearBeforeFill = true, Connection = new SqlConnection(connectionString) };
            adtImportStatisticsAll = new ImportStatisticsAllTableAdapter { ClearBeforeFill = true, Connection = new SqlConnection(connectionString) };
            adtExportStatistics = new ExportStatisticsTableAdapter { ClearBeforeFill = true, Connection = new SqlConnection(connectionString) };
            adtExportStatisticsEachDay = new ExportStatisticsEachDayTableAdapter { ClearBeforeFill = true, Connection = new SqlConnection(connectionString) };
            adtExportStatisticsAll = new ExportStatisticsAllTableAdapter { ClearBeforeFill = true, Connection = new SqlConnection(connectionString) };
            adtReceiptStatisticsEachDay = new ReceiptStatisticsEachDayTableAdapter { ClearBeforeFill = true, Connection = new SqlConnection(connectionString) };
            adtSellStatistics = new SellStatisticsTableAdapter { Connection = new SqlConnection(connectionString), ClearBeforeFill = true };
            adtSellStatistics_All = new SellStatistics_AllTableAdapter { Connection = new SqlConnection(connectionString), ClearBeforeFill = true };
            adtDebtStatistics = new DebtStatisticsTableAdapter { Connection = new SqlConnection(connectionString), ClearBeforeFill = true };
            adtDebtStatisticsAll = new DebtStatistics_AllTableAdapter { Connection = new SqlConnection(connectionString), ClearBeforeFill = true };
            adtDebtStatisticsCompany = new DebtStatistics_CompanyTableAdapter { Connection = new SqlConnection(connectionStr), ClearBeforeFill = true };
            adtExpensesStatistics = new ExpenseStatisticsTableAdapter { Connection = new SqlConnection(connectionString), ClearBeforeFill = true };
            adtStoreStatistics = new StoreStatisticsTableAdapter { Connection = new SqlConnection(connectionString), ClearBeforeFill = true };
            adtPaymentStatistic = new PaymentStatisticsTableAdapter { Connection = new SqlConnection(connectionString), ClearBeforeFill = true };

            adtNumOfDis = new NumOfDispensersTableAdapter { Connection = new SqlConnection(connectionString), ClearBeforeFill = true };

            //used
            adtIndexers = new IndexForDispensersTableAdapter { Connection = new SqlConnection(connectionString), ClearBeforeFill = true };
            adtGetUserList = new GetUserListTableAdapter { Connection = new SqlConnection(connectionString), ClearBeforeFill = true };
            if (autoLoadData)
            {
                loadData(Store);
            }
        }

        public void loadCompanies()
        {
            adtCompanies.Fill(dsOilManagement.Companies);
        }
        public void loadCustomers()
        {
            adtCustomers.Fill(dsOilManagement.Customers);
        }

        public void loadStores()
        {
            adtStores.Fill(dsOilManagement.Stores);
        }

        public void loadExpenses()
        {
            adtExpenses.Fill(dsOilManagement.Expenses);
        }

        public void loadProducts()
        {
            adtProducts.Fill(dsOilManagement.Products);
        }

        public void loadImport()
        {
            adtImports.Fill(dsOilManagement.Imports);
        }

        public void loadPayments()
        {
            adtPayments.Fill(dsOilManagement.Payments);
        }

        public void loadSells()
        {
            adtSells.Fill(dsOilManagement.Sells);
        }

        public void loadDebts()
        {
            adtDebts.Fill(dsOilManagement.Debts);
        }

        public void loadDispensers()
        {
            adtDis.Fill(dsOilManagement.Dispensers);
        }

        public void loadImportStatisticAll()
        {
            adtImportStatisticsAll.Fill(dsOilManagement.ImportStatisticsAll);
        }

        public void loadExportStatisticAll()
        {
            adtExportStatisticsAll.Fill(dsOilManagement.ExportStatisticsAll);
        }

        public void loadDebtStatisticsAll(Guid Store)
        {
            if (Store == Guid.Empty)
            {
                adtDebtStatisticsAll.Fill(dsOilManagement.DebtStatistics_All, null);
            }
            else
            {
                adtDebtStatisticsAll.Fill(dsOilManagement.DebtStatistics_All, Store);
            }
        }

        public void loadDebtStatisticsCompany(Guid Store)
        {
            if (Store == Guid.Empty)
            {
                adtDebtStatisticsCompany.Fill(dsOilManagement.DebtStatistics_Company, null);
            }
            else
            {
                adtDebtStatisticsCompany.Fill(dsOilManagement.DebtStatistics_Company, Store);
            }
        }

        public void loadNumOfDispensers()
        {
            adtNumOfDis.Fill(dsOilManagement.NumOfDispensers);
        }

        //public void loadIndexers()
        //{
        //    adtIndexers.Fill(dsOilManagement.IndexForDispensers);
        //}

        public Guid CurrentStore
        {
            get
            {
                return currentStore;
            }
        }

        public string CurrentStoreName
        {
            get
            {
                return currentStoreName;
            }
        }
        public void loadData(Guid Store)
        {
            currentStore = Store;
            adtCompanies.Fill(dsOilManagement.Companies);
            adtCustomers.Fill(dsOilManagement.Customers);
            adtStores.Fill(dsOilManagement.Stores);
            adtExpenses.Fill(dsOilManagement.Expenses);
            adtProducts.Fill(dsOilManagement.Products);
            adtImports.Fill(dsOilManagement.Imports);
            adtPayments.Fill(dsOilManagement.Payments);
            adtSells.Fill(dsOilManagement.Sells);
            adtDebts.Fill(dsOilManagement.Debts);
            adtDis.Fill(dsOilManagement.Dispensers);
            adtRec.Fill(dsOilManagement.Receipts);
            //statistic
            loadDebtStatisticsAll(Store);
            loadDebtStatisticsCompany(Store);
            adtImportStatisticsAll.Fill(dsOilManagement.ImportStatisticsAll);
            adtExportStatisticsAll.Fill(dsOilManagement.ExportStatisticsAll);
            adtNumOfDis.Fill(dsOilManagement.NumOfDispensers);
            if (currentStore != Guid.Empty)
            {
                DataSetOilManagement.StoresRow row = dsOilManagement.Stores.FindByID(currentStore);
                if (row != null)
                {
                    currentStoreName = row.StoreName;
                }
            }
            //used
            //adtIndexers.Fill(dsOilManagement.IndexForDispensers);
        }

        public string ConnectionString { get { return connectionStr; } }

        public bool Commit()
        {
            try
            {
                managerOfAdts.UpdateAll(dsOilManagement);
                return true;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return false;
                //dsOilManagement.RejectChanges();                
            }
        }

        public DataSetOilManagement DsOil { get { return dsOilManagement; } set { dsOilManagement = value; } }

        public bool ValidateAndUpdate_Imports(DataRowState state, params DataRow[] rows)
        {
            bool res = true;
            int n = rows.Length;
            switch (state)
            {
                case DataRowState.Added:
                    foreach (DataSetOilManagement.ImportsRow row in rows)
                    {
                        Guid productId = (Guid)row["Product"];
                        Guid storeId = (Guid)row["StoreID"];
                        decimal amount = (decimal)row["Amount"];

                        AddAnImport(productId, storeId, amount);
                    }
                    break;
                case DataRowState.Deleted:
                    foreach (DataSetOilManagement.ImportsRow row in rows)
                    {
                        Guid productId = (Guid)row["Product", DataRowVersion.Current];
                        Guid storeId = (Guid)row["StoreID", DataRowVersion.Current];
                        decimal amount = (decimal)row["Amount", DataRowVersion.Current];

                        RemoveAnImport(productId, storeId, amount);
                    }
                    foreach (DataRow row in dsOilManagement.ImportStatisticsAll.GetChanges().Rows)
                    {
                        Guid productId = (Guid)row["Product"];
                        Guid storeId = (Guid)row["StoreID"];
                        decimal ttIn = (decimal)row["TotalAmount"];
                        DataSetOilManagement.ExportStatisticsAllRow outrow = dsOilManagement.ExportStatisticsAll.FindByProductStoreID(productId, storeId);
                        if (outrow != null)
                        {
                            decimal ttOut = (decimal)outrow["TotalAmount"];
                            if (ttIn < ttOut)
                            {
                                res = false;
                                break;
                            }
                        }
                    }
                    break;
                case DataRowState.Detached:
                    break;
                case DataRowState.Modified:
                    foreach (DataSetOilManagement.ImportsRow row in rows)
                    {
                        Guid productId = (Guid)row["Product", DataRowVersion.Current];
                        Guid storeId = (Guid)row["StoreID", DataRowVersion.Current];
                        decimal amount = (decimal)row["Amount", DataRowVersion.Current];

                        RemoveAnImport(productId, storeId, amount);

                        productId = (Guid)row["Product", DataRowVersion.Proposed];
                        storeId = (Guid)row["StoreID", DataRowVersion.Proposed];
                        amount = (decimal)row["Amount", DataRowVersion.Proposed];

                        AddAnImport(productId, storeId, amount);
                    }
                    foreach (DataRow row in dsOilManagement.ImportStatisticsAll.GetChanges().Rows)
                    {
                        Guid productId = (Guid)row["Product"];
                        Guid storeId = (Guid)row["StoreID"];
                        decimal ttIn = (decimal)row["TotalAmount"];
                        DataSetOilManagement.ExportStatisticsAllRow outrow = dsOilManagement.ExportStatisticsAll.FindByProductStoreID(productId, storeId);
                        if (outrow != null)
                        {
                            decimal ttOut = (decimal)outrow["TotalAmount"];
                            if (ttIn < ttOut)
                            {
                                res = false;
                                break;
                            }
                        }
                    }
                    break;
                case DataRowState.Unchanged:
                    break;
                default:
                    break;
            }
            if (!res)
            {
                dsOilManagement.ImportStatisticsAll.RejectChanges();
            }
            else
            {
                dsOilManagement.ImportStatisticsAll.AcceptChanges();
            }
            return res;
        }

        public void RowsRejectChanges(params DataRow[] rows)
        {
            foreach (DataRow r in rows)
            {
                r.RejectChanges();
            }
        }

        private void AddAnImport(Guid productId, Guid storeId, decimal amount)
        {
            DataSetOilManagement.ImportStatisticsAllRow r = dsOilManagement.ImportStatisticsAll.FindByStoreIDProduct(storeId, productId);
            if (r != null)
            {
                r.TotalAmount += amount;
            }
            else
            {
                dsOilManagement.ImportStatisticsAll.AddImportStatisticsAllRow(storeId, productId, amount);
            }
        }

        private void RemoveAnImport(Guid productId, Guid storeId, decimal amount)
        {
            DataSetOilManagement.ImportStatisticsAllRow r = dsOilManagement.ImportStatisticsAll.FindByStoreIDProduct(storeId, productId);
            r.TotalAmount -= amount;
        }

        public bool ValidateAndUpdate_Sells(DataRowState state, params DataRow[] rows)
        {
            bool res = true;
            int n = rows.Length;
            switch (state)
            {
                case DataRowState.Added:
                    break;
                case DataRowState.Deleted:
                    foreach (DataSetOilManagement.SellsRow row in rows)
                    {
                        Guid productId = (Guid)row["Product", DataRowVersion.Current];
                        Guid storeId = (Guid)row["StoreID", DataRowVersion.Current];
                        decimal amount = (decimal)row["Amount", DataRowVersion.Current];
                        
                        RemoveAnExport(productId, storeId, amount);

                    }
                    break;
                case DataRowState.Detached:
                    break;
                case DataRowState.Modified:
                    foreach (DataSetOilManagement.SellsRow row in rows)
                    {
                        //remove
                        Guid productId = (Guid)row["Product", DataRowVersion.Current];
                        Guid storeId = (Guid)row["StoreID", DataRowVersion.Current];
                        decimal amount = (decimal)row["Amount", DataRowVersion.Current];

                        RemoveAnExport(productId, storeId, amount);

                        //add
                        productId = (Guid)row["Product", DataRowVersion.Proposed];
                        storeId = (Guid)row["StoreID", DataRowVersion.Proposed];
                        amount = (decimal)row["Amount", DataRowVersion.Proposed];

                        AddAnExport(productId, storeId, amount);
                       
                    }
                    foreach (DataRow row in dsOilManagement.ExportStatisticsAll.GetChanges().Rows)
                    {
                        Guid productId = (Guid)row["Product"];
                        Guid storeId = (Guid)row["StoreID"];
                        decimal ttOut = (decimal)row["TotalAmount"];
                        DataSetOilManagement.ImportStatisticsAllRow inrow = dsOilManagement.ImportStatisticsAll.FindByStoreIDProduct(storeId, productId);
                        if (inrow != null)
                        {
                            decimal ttIn = (decimal)inrow["TotalAmount"];
                            if (ttIn < ttOut)
                            {
                                res = false;
                                break;
                            }
                        }
                        else
                        {
                            res = false;
                            break;
                        }
                    }
                    break;
                case DataRowState.Unchanged:
                    break;
                default:
                    break;
            }
            if (!res)
            {
                dsOilManagement.ExportStatisticsAll.RejectChanges();
            }
            else
            {
                dsOilManagement.ExportStatisticsAll.AcceptChanges();
            }
            return res;
        }

        private void AddAnExport(Guid productId, Guid storeId, decimal amount)
        {
            DataSetOilManagement.ExportStatisticsAllRow r = dsOilManagement.ExportStatisticsAll.FindByProductStoreID(productId, storeId);
            if (r != null)
            {
                r.TotalAmount += amount;
            }
            else
            {
                dsOilManagement.ExportStatisticsAll.AddExportStatisticsAllRow(storeId, productId, amount);
            }
        }

        private void RemoveAnExport(Guid productId, Guid storeId, decimal amount)
        {
            DataSetOilManagement.ExportStatisticsAllRow r = dsOilManagement.ExportStatisticsAll.FindByProductStoreID(productId, storeId);
            r.TotalAmount -= amount;
        }

        private void RecalcDebt(Guid customerId, decimal debt)
        {
            //Debt Statistics_All
             DataSetOilManagement.DebtStatistics_AllRow r = dsOilManagement.DebtStatistics_All.FindByCustomer(customerId);
                if (r == null)
                {
                    r = dsOilManagement.DebtStatistics_All.NewDebtStatistics_AllRow();
                    r.Customer = customerId;
                    r.Debt = debt;
                    dsOilManagement.DebtStatistics_All.AddDebtStatistics_AllRow(r);
                }
                else
                {
                    r.Debt += debt;
                }
                if (r.Debt == (decimal)0)
                    dsOilManagement.DebtStatistics_All.RemoveDebtStatistics_AllRow(r);            
            //Debt Statistics_Company
                DataSetOilManagement.CustomersRow rCus = dsOilManagement.Customers.FindByID(customerId);
                if (rCus != null)
                {
                    Guid companyId = rCus.Company;
                    DataSetOilManagement.DebtStatistics_CompanyRow rDebtCom = dsOilManagement.DebtStatistics_Company.FindByCompany(companyId);
                    if (rDebtCom == null)
                    {
                        rDebtCom = dsOilManagement.DebtStatistics_Company.NewDebtStatistics_CompanyRow();
                        rDebtCom.Company = companyId;
                        rDebtCom.Debt = debt;
                        dsOilManagement.DebtStatistics_Company.AddDebtStatistics_CompanyRow(rDebtCom);
                    }
                    else
                    {
                        rDebtCom.Debt += debt;
                    }
                    if (rDebtCom.Debt == (decimal)0)
                    {
                        dsOilManagement.DebtStatistics_Company.RemoveDebtStatistics_CompanyRow(rDebtCom);
                    }
                }
        }

        public void Update_Payments(DataRowState state, params DataRow[] rows)
        {
            int n = rows.Length;
            switch (state)
            {
                case DataRowState.Added:
                    foreach (DataSetOilManagement.PaymentsRow row in rows)
                    {
                        Guid customerId = (Guid)row["Customer"];
                        decimal amount = (decimal)row["Amount"];

                        RecalcDebt(customerId, -amount);
                    }
                    break;
                case DataRowState.Deleted:
                    foreach (DataSetOilManagement.PaymentsRow row in rows)
                    {
                        Guid customerId = (Guid)row["Customer", DataRowVersion.Current];                       
                        decimal amount = (decimal)row["Amount", DataRowVersion.Current];

                        RecalcDebt(customerId, amount);                                           
                    }
                    break;
                case DataRowState.Detached:
                    break;
                case DataRowState.Modified:
                    foreach (DataSetOilManagement.PaymentsRow row in rows)
                    {
                        //remove
                        Guid customerId = (Guid)row["Customer", DataRowVersion.Current];
                        decimal amount = (decimal)row["Amount", DataRowVersion.Current];

                        RecalcDebt(customerId, amount);                       

                        //add
                        customerId = (Guid)row["Customer", DataRowVersion.Proposed];
                        amount = -(decimal)row["Amount", DataRowVersion.Proposed];

                        RecalcDebt(customerId, amount);
                    }
                    
                    break;
                case DataRowState.Unchanged:
                    break;
                default:
                    break;
            }
            dsOilManagement.DebtStatistics_All.AcceptChanges();    
        }

        public bool Validate_Delete_Debts(params DataRow[] rows)
        {
            foreach (DataRow r in rows)
            {
                try
                {
                    Guid customer = (Guid)r["Customer"];
                    decimal amount = (decimal)r["Amount"];
                    DataSetOilManagement.DebtStatistics_AllRow debt = dsOilManagement.DebtStatistics_All.FindByCustomer(customer);
                    if (debt == null || debt.Debt < amount)
                    {
                        return false;
                    }
                }
                catch (Exception)
                {
                    return false;
                }
            }
            return true;
        }

        public void Update_Debts(DataRowState state, params DataRow[] rows)
        {
            int n = rows.Length;
            switch (state)
            {
                case DataRowState.Added:
                    foreach (DataSetOilManagement.DebtsRow row in rows)
                    {
                        Guid customerId = (Guid)row["Customer"];
                        decimal amount = (decimal)row["Amount"];

                        RecalcDebt(customerId, amount);
                    }
                    break;
                case DataRowState.Deleted:
                    foreach (DataSetOilManagement.DebtsRow row in rows)
                    {
                        Guid customerId = (Guid)row["Customer", DataRowVersion.Current];
                        decimal amount = (decimal)row["Amount", DataRowVersion.Current];

                        RecalcDebt(customerId, -amount);
                    }
                    break;
                case DataRowState.Detached:
                    break;
                case DataRowState.Modified:
                    foreach (DataSetOilManagement.DebtsRow row in rows)
                    {
                        //remove
                        Guid customerId = (Guid)row["Customer", DataRowVersion.Current];
                        decimal amount = (decimal)row["Amount", DataRowVersion.Current];

                        RecalcDebt(customerId, -amount);

                        //add
                        customerId = (Guid)row["Customer", DataRowVersion.Proposed];
                        amount = (decimal)row["Amount", DataRowVersion.Proposed];

                        RecalcDebt(customerId, amount);
                    }

                    break;
                case DataRowState.Unchanged:
                    break;
                default:
                    break;
            }
            dsOilManagement.DebtStatistics_All.AcceptChanges();
        }
        public void UpdateStatistics(Guid Store)
        {            
            adtImportStatisticsAll.Dispose();
            adtExportStatisticsAll.Dispose();
            adtDebtStatisticsAll.Dispose();
            adtImportStatisticsAll = new ImportStatisticsAllTableAdapter { ClearBeforeFill = true, Connection = new SqlConnection(connectionStr) };
            adtExportStatisticsAll = new ExportStatisticsAllTableAdapter { ClearBeforeFill = true, Connection = new SqlConnection(connectionStr) };
            adtDebtStatisticsAll = new DebtStatistics_AllTableAdapter { ClearBeforeFill = true, Connection = new SqlConnection(connectionStr) };
            adtImportStatisticsAll.Fill(dsOilManagement.ImportStatisticsAll);
            adtExportStatisticsAll.Fill(dsOilManagement.ExportStatisticsAll);
            if (Store == Guid.Empty)
            {
                adtDebtStatisticsAll.Fill(dsOilManagement.DebtStatistics_All, null);
            }
            else
            {
                adtDebtStatisticsAll.Fill(dsOilManagement.DebtStatistics_All, Store);
            }
        }
    }
}
